/*
 * Copyright (c) 2016. 恒昌互联网运营中心.
 */
package com.credithc.eagleeye.console.web;
import com.credithc.eagleeye.console.service.SettingService;
import com.credithc.eagleeye.console.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private SettingService settingService;
    private static final String AUTH_CONFIG_PATH = "property/auth-config.properties";
    @RequestMapping(value = "doLogin", method = RequestMethod.POST)
    @ResponseBody
    public String doLogin(HttpServletRequest request, String iUserName, String iPassword) {
        UserVo userVo = settingService.loadUser(iUserName);
        if(userVo!=null&& StringUtils.equals(iPassword,userVo.getPassword())){
            request.getSession().setAttribute("user", iUserName);
            return "1";
        }
//
//        Properties properties = PropertiesUtils.loadLocalProperties(AUTH_CONFIG_PATH);
//        String password = properties.getProperty(iUserName + ".password");
//        if (password != null && password.equals(iPassword)) {
//            request.getSession().setAttribute("user", iUserName);
//            return "1";
//        }
        return "-1";
    }
    @RequestMapping(value = "cancel", method = RequestMethod.POST)
    public void index(HttpServletRequest request, HttpServletResponse response) {
        try {
            request.getSession().removeAttribute("user");
            response.sendRedirect("../login.jsp");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
